﻿Imports System.Drawing.Drawing2D
Imports System.ComponentModel

Public Class Login : Inherits UserControl

	Dim ErrMsg As String

	Dim Users As New SortedList(Of String, Login)
	Dim FocusedLogin As Login
	Dim Watch As Stopwatch
	Dim AniRect As Rectangle
	Dim ShowControls As Boolean = False
	Dim FocusedControl As Control
	Dim RsUsers As Recordset

	Dim _SelectedLogin As Login

	Event LoginSuccessful(sender As Object)

	Property SelectedLogin As Login
		Get
			Return _SelectedLogin
		End Get
		Set(ByVal Value As Login)
			If Not SelectedLogin Is Nothing Then Animator.Run(SelectedLogin, "Margin", 0, 8, 0.2, 0)
			_SelectedLogin = Value
			If Not SelectedLogin Is Nothing Then Animator.Run(SelectedLogin, "Margin", 0, 8, 0.2, 8)
		End Set
	End Property

	Dim _HoveredLogin As Login
	Property HoveredLogin As Login
		Get
			Return _HoveredLogin
		End Get
		Set(ByVal Value As Login)
			If Not HoveredLogin Is Nothing Then Animator.Run(HoveredLogin, "Margin", 0, 8, 0.2, If(HoveredLogin Is SelectedLogin, 8, 0))
			_HoveredLogin = Value
			If Not HoveredLogin Is Nothing Then Animator.Run(HoveredLogin, "Margin", 0, 8, 0.2, 8)
		End Set
	End Property

	Public WithEvents UserName As New ComboBox
	Public WithEvents Password As New TextBox

	Public Sub New()
		Themes.Register(Me)
		BackgroundImage = My.Resources.defaultback
		BackgroundImageLayout = ImageLayout.Stretch
		Controls.Add(UserName)
		Controls.Add(Password)
		UserName.Visible = False
		Password.Visible = False
		Password.PasswordChar = "*"
		UserName.BackColor = Color.White
		Password.BackColor = Color.White
	End Sub

	Enum InterfaceMode
		None
		Choose
		Password
		Connect
	End Enum

	Protected Overrides Sub OnLoad(e As System.EventArgs)
		MyBase.OnLoad(e)
		Dim StartupUser As String = ""
		Dim StartupPassword As String = ""
		For Each Item In My.Application.CommandLineArgs
			If Item.StartsWith("/u:") Then StartupUser = Item.Mid(4).Trim
			If Item.StartsWith("/p:") Then StartupPassword = Item.Mid(4).Trim
		Next
		Mode = InterfaceMode.Connect
		Watch = Stopwatch.StartNew
		Invalidate()
		Application.DoEvents()
		Worker = New BackgroundWorker
		Do
			Worker.RunWorkerAsync()
			Do Until Not Worker.IsBusy
				Invalidate(AniRect)
				Application.DoEvents()
			Loop
			Watch.Stop()
			If ErrMsg = "" Then Exit Do
			If Not Confirm("Sunucu ile bağlantı kurulamadı. Tekrar denemek ister misiniz?", ErrMsg) Then End
		Loop
		RsUsers = User.GetUsers
		Dim Rs As Recordset = RsUsers.GroupBy("kaykul,adi")
		UserName.Source = Rs
		UserName.SortBy("ad")
		Mode = InterfaceMode.Choose
		ReadUserList()
		'If StartupUser <> "" AndAlso StartupPassword <> "" Then
		'	CheckUser(StartupUser, StartupPassword)
		'End If
	End Sub

	Dim m_Mode As InterfaceMode = InterfaceMode.Choose
	Property Mode() As InterfaceMode
		Get
			Return m_Mode
		End Get
		Set(ByVal Value As InterfaceMode)
			Application.DoEvents()
			If Mode = Value Then Exit Property
			Cursor = Cursors.Default
			If Mode = InterfaceMode.Password Then
				UserName.Visible = False
				Password.Visible = False
				Focus()
			End If
			m_Mode = Value
			ShowControls = Mode = InterfaceMode.Password
			Invalidate()
		End Set
	End Property

	Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
		Select Case Mode
			Case InterfaceMode.Choose
				Dim Selected As Integer = Users.IndexOfValue(SelectedLogin)
				Select Case keyData
					Case Keys.Right, Keys.Down
						If Selected >= Users.Count - 1 Then Exit Function
						SelectedLogin = Users.Values(Selected + 1)
					Case Keys.Left, Keys.Up
						If Selected = 0 Then Exit Function
						SelectedLogin = Users.Values(Selected - 1)
					Case Keys.Enter
						If Selected < 0 Then Exit Function
						EnterPasswordMode()
					Case Keys.Escape
						FindForm.Close()
						Return True
					Case Keys.Delete
						Delete()
				End Select
				Return MyBase.ProcessDialogKey(keyData)
			Case InterfaceMode.Password
				Select Case keyData
					Case Keys.Tab
						If ActiveControl Is UserName Then Password.Focus() Else CheckUser()
						Return True
					Case Keys.Escape
						Mode = InterfaceMode.Choose
						Return True
				End Select
		End Select
		Return MyBase.ProcessDialogKey(keyData)
	End Function

	Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
		MyBase.OnKeyDown(e)
		Select Case Mode
			Case InterfaceMode.Choose
				If e.KeyCode = Keys.Escape Then e.Handled = True
		End Select
	End Sub

	Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
		MyBase.OnKeyPress(e)
		If e.KeyChar = Chr(27) Then e.Handled = True
	End Sub

#Region " Mouse events "

	Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
		MyBase.OnMouseDown(e)
		Select Case Mode
			Case InterfaceMode.Choose
				If HoveredLogin Is Nothing Then Exit Sub
				SelectedLogin = HoveredLogin
		End Select
	End Sub

	Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
		MyBase.OnMouseMove(e)
		Select Case Mode
			Case InterfaceMode.Choose
				Dim FoundLogin As Login = Nothing
				For Each Login As Login In Users.Values
					If Login.ImageRect.Contains(e.Location) OrElse Login.TextRect.Contains(e.Location) Then FoundLogin = Login : Exit For
				Next
				If HoveredLogin Is FoundLogin Then Exit Sub
				HoveredLogin = FoundLogin
				Cursor = If(HoveredLogin Is Nothing, Cursors.Default, Cursors.Hand)
			Case InterfaceMode.Password
				Cursor = Cursors.Default
			Case InterfaceMode.Connect
				Cursor = Cursors.AppStarting
		End Select
	End Sub

	Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
		MyBase.OnMouseUp(e)
		Select Case Mode
			Case InterfaceMode.Choose
				If HoveredLogin Is Nothing Then Exit Sub
				SelectedLogin = HoveredLogin
				If e.Button = Windows.Forms.MouseButtons.Left Then
					EnterPasswordMode()
					Invalidate()
				Else
					FocusedLogin = SelectedLogin
					Password.Visible = False
					Invalidate()
				End If
		End Select
	End Sub

#End Region

	Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
		MyBase.OnPaint(e)
		Dim G As Graphics = e.Graphics
		If Width = 0 Or Height = 0 Then Exit Sub
		G.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
		G.SmoothingMode = SmoothingMode.None
		DrawAmblem(G)
		Redraw(G)
	End Sub

	Sub DrawTitle(ByVal G As Graphics)
		Dim R As New Rectangle(0, 0, Width, 36)
		Themes.Current.Blur(G, Me, R)
		G.FillRectangle(New SolidBrush(Color.FromArgb(40, Color.Black)), R)
		G.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
		R.Inflate(-8, 0)
		DrawStringShadowed(G, "Sosyal Sigortalar Dairesi Merkez ve Şube Uygulamaları", New Font("segoe ui", 12, FontStyle.Regular), Color.White, R, StrFormat("l", "c"))
	End Sub

	Sub DrawAmblem(ByVal G As Graphics)
		Static B As Bitmap
		If B Is Nothing Then
			B = My.Resources.amblem.Clone
			For x As Integer = 0 To B.Width - 1
				For y As Integer = 0 To B.Height - 1
					Dim c As Color = B.GetPixel(x, y)
					B.SetPixel(x, y, Color.FromArgb(255 - c.R, Color.White))
				Next
			Next
		End If
		Dim R As New Rectangle(Width \ 2, 0, 0, 0)
		R.Inflate(40, 40)
		R.Offset(0, R.Height \ 2 + 16)
		G.InterpolationMode = InterpolationMode.HighQualityBicubic
		G.DrawImage(B, R)
		G.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
		R.Offset(0, R.Width \ 2)
		R.Offset(0, 36)
		R.Inflate(Width \ 2, 0)
		Dim F As New StringFormat
		F.LineAlignment = StringAlignment.Center
		F.Alignment = StringAlignment.Center
		Dim Font As New Font("arial", 15)
		DrawStringShadowed(G, "ÇALIŞMA VE SOSYAL GÜVENLİK BAKANLIĞI", Font, Color.White, R, F)
	End Sub

	Sub PaintControl(ByVal G As Graphics, ByVal Control As Control, ByVal Caption As String)
		Dim Focused As Boolean = FocusedControl Is Control
		Dim R As Rectangle = Control.Bounds
		G.SmoothingMode = SmoothingMode.None
		G.PixelOffsetMode = PixelOffsetMode.None
		R.Offset(-1, -1)
		'Themes.Current.Blur(G, Me, R)
		R.Height += 1 : R.Width += 1
		'R.Inflate(-2, -2)
		G.FillRectangle(Color.White.Opacity(If(Focused, 255, 64)).Brush, R)
		R.Inflate(2, 2)
		R.Height -= 1 : R.Width -= 1
		G.DrawRectangle(Color.White.Opacity(If(Focused, 255, 64)).Pen, R)
		If Focused Then
			'Dim ImgRect As Rectangle = R
			'ImgRect.Width = ImgRect.Height
			'ImgRect.Offset(-ImgRect.Width, 0)
			'ImgRect.Inflate(-4, -4)
			'ImgRect.Offset(1, 1)
			'Dim B As Image = My.Resources.bwsearch
			'B.RotateFlip(RotateFlipType.RotateNoneFlipX)
			'DrawImage(G, B, ImgRect, 0.8)
		Else
			Dim ImgRect As Rectangle = R
			ImgRect.Width = ImgRect.Height
			ImgRect.Offset(R.Width - ImgRect.Width, 0)
			ImgRect.Inflate(-4, -4)
			ImgRect.Offset(1, 1)
			Dim B As Image = My.Resources.bwsearch
			B.RotateFlip(RotateFlipType.RotateNoneFlipX)
			DrawImage(G, B, ImgRect, 0.8)
			R.Offset(4, 0)
			DrawStringShadowed(G, Caption, Control.Font, Color.FromArgb(If(FocusedControl Is Control, 255, 255), Color.White), R, StrFormat("l", "c"))
		End If
	End Sub

	Sub ShowInfo(ByVal G As Graphics, ByVal Text As String)
		Dim Font As New Font("segoe ui", 10, FontStyle.Bold)
		DrawStringShadowed(G, Text, Font, Color.White, New Rectangle(0, Height - Height / 8, Width, Height / 8), Centered, 0)
	End Sub


	Sub Redraw(ByVal G As Graphics)
		Dim MaxIconSize As Integer = 60
		Dim MaxColumns As Integer = 3
		Dim MaxUsersColumn As Integer = 4
		Dim ColumnWidth As Integer = 240
		G.SmoothingMode = SmoothingMode.HighQuality
		G.PixelOffsetMode = PixelOffsetMode.HighQuality
		G.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
		Dim MinY As Integer = 200
		Dim MaxY As Integer = Height - Height / 8 - 20 - Height / 32
		If Users.Count > 0 Then
			Dim ColumnCount As Integer = Int((Users.Count + MaxUsersColumn - 1) / MaxUsersColumn)
			Dim UsersOnColumn As Integer = MaxUsersColumn
			If ColumnCount = 1 Then
				UsersOnColumn = Users.Count
			ElseIf ColumnCount > 3 Then
				ColumnCount = 3
				UsersOnColumn = Int(Users.Count / 3 + 0.99)
			End If
			If Users.Count Mod UsersOnColumn = 1 Then UsersOnColumn -= 1
			Dim IconSize = 4 * (MaxY - MinY) / (4 * UsersOnColumn - 1)
			If IconSize > MaxIconSize Then IconSize = MaxIconSize
			If IconSize < 12 Then IconSize = 12
			Dim IconSpacing = IconSize / 4
			Select Case Mode
				Case InterfaceMode.Choose
					ShowInfo(G, "Adınızı seçip parolanızı girin. Adınız listede yoksa, ""Diğer kullanıcı"" seçeneğini kullanın.")
					Dim i As Integer = 0
					Dim R As Rectangle
					For Each Login As Login In Users.Values
						If i Mod UsersOnColumn = 0 Then
							Dim c As Integer = Int(i / UsersOnColumn)
							Dim x As Integer = Width / 2 - ColumnWidth * (ColumnCount / 2 - c) + ColumnWidth / 6
							Dim y As Integer = (MinY + MaxY) \ 2 - (UsersOnColumn * IconSize + (UsersOnColumn - 1) * IconSpacing) \ 2
							R = New Rectangle(x, y, IconSize, IconSize)
						End If
						Login.Draw(G, R, Me, Login Is SelectedLogin, True)
						R.Offset(0, IconSize + IconSpacing)
						i += 1
					Next
				Case InterfaceMode.Password
					If SelectedLogin.Type = "other" Then
						ShowInfo(G, "Listeden adınızı seçin ve parolanızı girin.")
					Else
						ShowInfo(G, "Uygulamaya giriş yapmak için parolanızı girin.")
					End If
					Dim x As Integer = Width / 2 - ColumnWidth * (1 / 2) + ColumnWidth / 6
					Dim y As Integer = (MinY + MaxY) \ 2 - IconSize \ 2
					Dim R As New Rectangle(x, y, IconSize, IconSize)
					R.Inflate(8, 8)
					SelectedLogin.Draw(G, R, Me, True, False)
					G.PixelOffsetMode = PixelOffsetMode.None
					Dim Pr As New Rectangle(R.Right + 12, R.Top, 160, 22)
					If SelectedLogin.Type = "other" Then
						Pr.Offset(0, (R.Height \ 2 - Pr.Height) \ 2)
						UserName.Location = Pr.Location
						Pr.Width -= 4
						UserName.Size = Pr.Size
						Pr.Width += 4
						If ShowControls Then
							UserName.Reset()
							UserName.Visible = True
						End If
						Pr.Width -= 1
						Pr.Inflate(-1, -1)
						Pr.Offset(-2, -2)
						G.FillRectangle(Brushes.White, Pr)
						Pr.Inflate(1, 1)
						G.DrawRectangle(New Pen(Color.FromArgb(192, Color.White)), Pr)
						'Pr.Offset(2, 2)
					End If
					Pr.Offset(0, R.Height \ 2)
					Pr.Offset(2, 2)
					Password.Location = Pr.Location
					Pr.Width -= 4
					Password.Size = Pr.Size
					Pr.Width += 4
					Pr.Offset(-2, -2)
					If ShowControls Then
						Password.Text = ""
						Password.Visible = True
					End If
					Pr.Inflate(-1, -1)
					G.FillRectangle(Brushes.White, Pr)
					Pr.Inflate(1, 1)
					G.DrawRectangle(New Pen(Color.FromArgb(192, Color.White)), Pr)
					Dim IR As New Rectangle(Pr.Right, Pr.Top, Pr.Height, Pr.Height)
					IR.Inflate(-2, -2)
					DrawImage(G, My.Resources.user, IR)
					If ShowControls Then
						If UserName.Visible Then UserName.Focus() Else Password.Focus()
						ShowControls = False
					End If
				Case InterfaceMode.Connect
					Dim Pr As New Rectangle(0, Height / 2 - 16, Width, 32)
					DrawStringShadowed(G, "Bağlantı kuruluyor...", New Font("segoe ui", 17), Color.White, Pr, Centered)
					Pr = New Rectangle(0, Height / 2 + 28, 1, 1)
					Pr.Inflate(24, 24)
					Dim Center As New Point(Width / 2, Pr.Bottom + 32)
					Dim rd As Single = 24
					AniRect = New Rectangle(Center, New Size(0, 0))
					AniRect.Inflate(rd + 6, rd + 6)
					AniRect.Inflate(rd, rd)
					For ang As Single = 0 To 359 Step 30
						Pr = New Rectangle(Center.X + rd * Math.Cos(ang * Math.PI / 180), Center.Y + rd * Math.Sin(ang * Math.PI / 180), 1, 1)
						Pr.Inflate(4, 4)
						Dim alpha As Integer = 255 - ((Watch.ElapsedMilliseconds / 4 + 255 - ang * 255 / 360) Mod 255)
						G.FillEllipse(New SolidBrush(Color.FromArgb(alpha, Color.White)), Pr)
						G.DrawEllipse(New Pen(Color.FromArgb(alpha / 4, Color.Black), 2), Pr)
					Next
					G.PixelOffsetMode = PixelOffsetMode.HighQuality
			End Select
		End If
	End Sub

	Sub ReadUserList()
		Dim UserList As String = GetSetting("SSD", "Users", "List", "")
		Dim LastUser As String = GetSetting("SSD", "Users", "Last", "")
		For Each Item As Object In Split("admin;test;ssd;misafir", ";")
			UserList = Replace(UserList, "<" & Item & ">", "")
		Next
		Users.Clear()
		If UserList Is Nothing Then UserList = ""
		UserList = UserList.Replace("><", "!").Replace(">", "").Replace("<", "")
		For Each Item As Object In Split(Lower(UserList), "!")
			If Item <> "" AndAlso Not Users.ContainsKey(Item) Then
				Dim Rs As Recordset = RsUsers.Select("username='" & Item & "'")
				If Not Rs.EOF Then Users.Add(Item, New Login(Me, "", Item, Rs("adi")))
			End If
		Next
		Users.Add("zzzyeni", New Login(Me, "other", "Diğer", "Diğer kullanıcı"))
		Select Case LCase(My.Computer.Name)
			Case "ssd-ahmet", "ssd-kemal", "shuttle", "samsung", "ssdlefkosa"
				Users.Add("!!test", New Login(Me, "test", "test", "Test kullanıcısı"))
				LastUser = "!!test"
		End Select
		If Users.ContainsKey(LastUser) Then SelectedLogin = Users(LastUser)
		Invalidate()
	End Sub

	Private Sub EnterPasswordMode()
		Select Case SelectedLogin.Type
			Case "test", "guest"
				CheckUser()
			Case Else
				FocusedLogin = SelectedLogin
				Mode = InterfaceMode.Password
				ShowControls = True
		End Select
	End Sub

	Private Sub CheckUser(Optional UserID As String = "", Optional Pwd As String = "")
		If UserID = "" Then
			SelectedLogin.Password = Password.Text
			If UserName.Visible Then
				If UserName.Value Is Nothing OrElse Len(UserName.Value) <> 4 Then UserName.Focus() : Warning("Geçerli bir ad seçmelisiniz.") : Exit Sub
				SelectedLogin.LoginAs = RsUsers.Select("kaykul='" & UserName.Value & "'")("username")
			End If
			UserID = SelectedLogin.User
			Pwd = SelectedLogin.Password
		End If
		Dim ErrMsg As String = User.Open(UserID, Pwd)
		If ErrMsg = "" Then
			Dim UserList As String = GetSetting("SSD", "Users", "List", "")
			UserList = Replace(UserList, "<" & SelectedLogin.User & ">", "") & "<" & SelectedLogin.User & ">"
			SaveSetting("SSD", "Users", "List", UserList)
			SaveSetting("SSD", "Users", "Last", SelectedLogin.User)
		End If
		If ErrMsg <> "" Then
			Warning(ErrMsg)
			Mode = InterfaceMode.Password
			Exit Sub
		End If
		Themes.Current = New DefaultTheme
		'Themes.Current.SetBackground(FindForm, 0)
		Themes.Register(Me)
		FrmMain.Tabs.Visible = True
		RaiseEvent LoginSuccessful(Me)
	End Sub

	Private Sub Password_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Password.GotFocus
		Password.SelectAll()
	End Sub

	Private Sub Password_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Password.KeyDown
		Select Case e.KeyCode
			Case Keys.Enter, Keys.Escape
				e.Handled = True
		End Select
	End Sub

	Private Sub Password_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Password.KeyPress
		Select Case e.KeyChar
			Case Chr(13), Chr(27)
				e.Handled = True
		End Select
	End Sub

	Private Sub Delete()
		If Not Confirm("Bu öğeyi silmek istediğinizden emin misiniz?") Then Exit Sub
		SaveSetting("SSD", "Users", "List", Replace(GetSetting("SSD", "Users", "List", ""), "<" & SelectedLogin.Id & ">", "", 1, -1, CompareMethod.Text))
		ReadUserList()
	End Sub

#Region " Async connection "

	Dim WithEvents Worker As BackgroundWorker
	Private Sub Worker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Worker.DoWork
		Database.Initialize(0)
	End Sub

#End Region

	Class Login
		Public ImageRect As Rectangle
		Public TextRect As Rectangle
		Public Type As String
		Public LoginAs As String
		Public DisplayName As String
		Public Id As String
		Public Parent As Control

		Sub New(Parent As Control, ByVal Type As String, ByVal Id As String, ByVal DisplayName As String)
			Me.Parent = Parent
			Me.Type = Type
			Me.Id = Id
			Me.DisplayName = DisplayName
		End Sub

		ReadOnly Property User() As String
			Get
				Select Case Type
					Case "test" : Return "admin"
					Case "other" : Return LoginAs
					Case Else : Return Lower(Id)
				End Select
			End Get
		End Property

		Dim m_Password As String
		Property Password() As String
			Get
				Select Case Type
					Case "test" : Return "admin"
					Case Else : Return m_Password
				End Select
			End Get
			Set(ByVal Value As String)
				m_Password = Value
			End Set
		End Property

		Dim _Margin As Double = 0
		Property Margin As Double
			Get
				Return _Margin
			End Get
			Set(value As Double)
				_Margin = value
				Invalidate()
			End Set
		End Property

		Sub Invalidate()
			Dim R As Rectangle = ImageRect
			'R.Inflate(12, 12)
			Parent.Invalidate(R)
			Parent.Invalidate(TextRect)
		End Sub

		Sub DrawPicture(ByVal G As Graphics, ByVal m As Integer, ByVal R As Rectangle, ByVal Opacity As Double)
			R.Inflate(-R.Width * m / 64, -R.Height * m / 64)
			R.Inflate(-R.Width \ 16, -R.Height \ 16)
			Dim B As Bitmap = My.Resources.user
			DrawImage(G, B, R, Opacity)
		End Sub

		Sub DrawBorder(ByVal G As Graphics, ByVal R As Rectangle, ByVal C As Control)
			G.InterpolationMode = InterpolationMode.Bicubic
			Themes.Current.Blur(G, C, R)
			For j As Integer = 1 To 5
				DrawRoundRect(G, R, New Pen(Color.FromArgb((6 - j) * 10, Color.Black), j), (j + 3) * 2)
				R.Inflate(1, 1)
			Next
			R.Inflate(-5, -5)
		End Sub

		Sub Draw(ByVal G As Graphics, ByVal R As Rectangle, ByVal C As Control, ByVal IsSelected As Boolean, ByVal TextRight As Boolean)
			Dim Opacity As Single = 0.3 + Margin / 16
			ImageRect = R
			DrawBorder(G, ImageRect, C)
			Dim Brush = Color.White
			Dim ExtraDepth As Integer = 0
			'R.Inflate(12 - Margin, 12 - Margin)
			If IsSelected Then
				FillRoundRect(G, R, New SolidBrush(Color.FromArgb(160, Color.Black)), 1)
				Brush = Color.Yellow
				ExtraDepth += 1
			ElseIf Margin >= 0 Then
				FillRoundRect(G, R, New SolidBrush(Color.FromArgb(Margin * 8, Color.White)), 1)
			Else
				FillRoundRect(G, R, New SolidBrush(Color.FromArgb(32, Color.Black)), 1)
			End If
			G.PixelOffsetMode = PixelOffsetMode.HighQuality
			DrawRoundRect(G, R, New Pen(Color.FromArgb(255, Color.White), 2), 1)
			DrawPicture(G, 12 - Margin, R, Opacity)
			Brush = Color.FromArgb(255, 255, 255 - Margin * 31)
			R.Offset(R.Width + 12, 0)
			TextRect = R
			TextRect.Width = G.MeasureString(DisplayName, H1Font).Width + 16
			If Not TextRight Then TextRect.Height \= 2
			If Not (Id = "zzzyeni" And Not TextRight) Then
				DrawStringShadowed(G, DisplayName, H1Font, Brush, TextRect, StrFormat("l", "c"), ExtraDepth)
			End If
		End Sub

	End Class
End Class

